昨天設定好計時器的PickerView了,今天來增加其中一個Button的功能吧。
這裡是用跟碼表一樣的方式來切換Button的狀態,在按下開始的同時隱藏PickerView,將Label顯示出來。
    @IBAction func StartOrStop(_ sender: Any) {
        if timeInterval == 0{
            timeInterval = (hour * 3600 + minute * 60 + second) * 100
        }
        if startStauts {
            startStauts = false
            
            TimerPickerView.isHidden = true
            
            TimeLabel.isHidden = false
            
            StartStopButton.setTitle("暫停", for: .normal)
            StartStopButton.setTitleColor(.red, for: .normal)
            
            CancelButton.isEnabled = true
            CancelButton.setTitleColor(.black, for: .normal)
            
            timer = Timer.scheduledTimer(timeInterval: TimeInterval(0.01), target: self, selector: #selector(countDown), userInfo: nil, repeats: true)
        }else{
            startStauts = true
            
            StartStopButton.setTitle("繼續", for: .normal)
            StartStopButton.setTitleColor(.green, for: .normal)
            
            timer.invalidate()
        }
    }
    
這裡的Timer的函式設定成當秒數不為零的時候才會動作。
    @objc func countDown(){
        if timeInterval != 0{
            timeInterval -= 1
            
            calcuSec = (timeInterval / 100) % 60
            calcuMin = (timeInterval / 6000) % 60
            calcuHor = (timeInterval / 360000) % 24
            
            TimeLabel.text = String(format: "%02d:%02d:%02d", arguments: [calcuHor, calcuMin, calcuSec])
        }
到這裡就是今天的進度了,明天繼續幫另外一個Button加上功能,然後補上alert。